package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.entity.neww.PwkCg;
import cn.stylefeng.guns.dao.entity.neww.PwkKc;
import cn.stylefeng.guns.dao.entity.neww.PwkMdXs;
import cn.stylefeng.guns.dao.entity.neww.PwkXs;
import cn.stylefeng.guns.dao.service.neww.IPwkCgService;
import cn.stylefeng.guns.dao.service.neww.IPwkKcService;
import cn.stylefeng.guns.dao.service.neww.IPwkMdXsService;
import cn.stylefeng.guns.dao.service.neww.IPwkXsService;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.office.api.OfficeExcelApi;
import cn.stylefeng.roses.kernel.office.api.pojo.report.ExcelExportParam;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author quspacedraon
 * @since 2024-02-20
 */
@RestController
@ApiResource(name = "新的药德数据源", resBizType = ResBizTypeEnum.BUSINESS, path = "/yaode")
@Slf4j
public class NewYaodeController {
    @Resource
    private IPwkCgService pwkCgService;
    @Resource
    private IPwkKcService pwkKcService;
    @Resource
    private IPwkMdXsService pwkMdXsService;
    @Resource
    private IPwkXsService pwkXsService;
    @Resource
    private OfficeExcelApi officeExcelApi;

    @GetResource(
            name = "采购导出",
            path = {"/pwk/cg/export"}
    )
    @BusinessLog
    public void export(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, HttpServletResponse response) throws IOException {
        List<PwkCg> sysUserList = pwkCgService.list(Wrappers.<PwkCg>lambdaQuery()
                .ge(PwkCg::get时间, new Date(startTime)).le(PwkCg::get时间, new Date(endTime))
        );
        export(sysUserList, response, PwkCg.class, "采购导出");
    }

    @GetResource(name = "采购列表", path = "/pwk/cg", requiredLogin = true, requiredPermission = true)
    @ApiOperation("采购列表")
    public ResponseData<PageResult<PwkCg>> stockVenderList(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, @RequestParam("pageNo") Integer pageNo,
                                                           @RequestParam("pageSize") Integer pageSize) {
        IPage<PwkCg> page = new Page<>(pageNo, pageSize);
        IPage<PwkCg> page1 = pwkCgService.page(page, Wrappers.<PwkCg>lambdaQuery()
                .ge(PwkCg::get时间, new Date(startTime)).le(PwkCg::get时间, new Date(endTime))
                .orderBy(true, false, PwkCg::get时间)
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1.getRecords(), page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }


    private void export(List sysUserList, HttpServletResponse response, Class clazz, String fileName) {
        ExcelExportParam excelExportParam = new ExcelExportParam();
        response.setHeader("Content-Disposition", "attachment;filename=" + "sale.xlsx");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), clazz).build()) {
            WriteSheet writeSheetYeah = EasyExcel.writerSheet(fileName).build();

            excelWriter.write(sysUserList, writeSheetYeah);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetResource(
            name = "库存导出",
            path = {"/pwk/kc/export"}
    )
    @BusinessLog
    public void export2(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, HttpServletResponse response) {
        List<PwkKc> sysUserList = pwkKcService.list(Wrappers.<PwkKc>lambdaQuery()
                .ge(PwkKc::get日期, new Date(startTime))
                .le(PwkKc::get日期, new Date(endTime))
        );
        export(sysUserList, response, PwkKc.class, "总部库存");
    }

    @GetResource(name = "库存列表", path = "/pwk/kc", requiredLogin = true, requiredPermission = true)
    @ApiOperation("库存列表")
    public ResponseData<PageResult<PwkKc>> stockKcList(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        IPage<PwkKc> page = new Page<>(pageNo, pageSize);
        IPage<PwkKc> page1 = pwkKcService.page(page, Wrappers.<PwkKc>lambdaQuery().orderBy(true, false, PwkKc::get日期)
                .ge(PwkKc::get日期, new Date(startTime))
                .le(PwkKc::get日期, new Date(endTime))
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1.getRecords(), page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }

    @GetResource(
            name = "门店纯销导出",
            path = {"/pwk/md/xs/export"}
    )
    @BusinessLog
    public void export3(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, HttpServletResponse response) {
        List<PwkMdXs> sysUserList = pwkMdXsService.list(Wrappers.<PwkMdXs>lambdaQuery()
                .ge(PwkMdXs::get销售时间, new Date(startTime))
                .le(PwkMdXs::get销售时间, new Date(endTime))
        );
        export(sysUserList, response, PwkMdXs.class, "门店纯销");
    }

    @GetResource(name = "纯销列表", path = "/pwk/md/xs", requiredLogin = true, requiredPermission = true)
    @ApiOperation("库存列表")
    public ResponseData<PageResult<PwkMdXs>> stockxsList(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        IPage<PwkMdXs> page = new Page<>(pageNo, pageSize);
        IPage<PwkMdXs> page1 = pwkMdXsService.page(page, Wrappers.<PwkMdXs>lambdaQuery()
                        .orderBy(true, false, PwkMdXs::get销售时间)
                .ge(PwkMdXs::get销售时间, new Date(startTime))
                .le(PwkMdXs::get销售时间, new Date(endTime))
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1.getRecords(), page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }

    @GetResource(
            name = "销售导出",
            path = {"/pwk/xs/export"}
    )
    @BusinessLog
    public void export4(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, HttpServletResponse response) {
        List<PwkXs> sysUserList = pwkXsService.list(Wrappers.<PwkXs>lambdaQuery()
                .ge(PwkXs::get时间, new Date(startTime)).le(PwkXs::get时间, new Date(endTime))
        );
        export(sysUserList, response, PwkXs.class, "总部配送");
    }

    @GetResource(name = "销售列表", path = "/pwk/xs", requiredLogin = true, requiredPermission = true)
    @ApiOperation("库存列表")
    public ResponseData<PageResult<PwkXs>> stockxsLis11t(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        IPage<PwkXs> page = new Page<>(pageNo, pageSize);
        IPage<PwkXs> page1 = pwkXsService.page(page, Wrappers.<PwkXs>lambdaQuery()
                        .orderBy(true, false, PwkXs::get时间)
                .ge(PwkXs::get时间, new Date(startTime)).le(PwkXs::get时间, new Date(endTime))
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1.getRecords(), page.getTotal(), (int) page.getSize(), (int) page.getCurrent()));
    }


}
